<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--导入CSS样式-->
    <link rel="stylesheet" type="text/css" href="css/skin.css" />
    <script type="text/javascript">
        function logout() {
            if(window.confirm('您确定要退出吗？')) {
                top.location = 'login';
            }
        }
    </script>
</head>
<body>
<!--左边导航栏宽度-->
<div width="500px" id="top" th:include="/top :: top"></div>
<div id="lief" th:include="/lief :: lief"></div>
<div id="chaxun">
    <table width="800px">
        <tr>
            <td colspan="2">
                <form action="" method="">
                    <p>
                        论文主题 : <input type="text"/>
                        论文类型
                        <select id="select">
                            <option>--请选择--</option>
<!--                            <option>应用性</option>-->
<!--                            <option>理论性</option>-->
                        </select>
                        <input type="submit" id="btn">查询</input>
                        <a href="add">添加论文</a>
                    </p>
                    <table  width="100%"  class="cont tr_color">
                        <thead>
                            <tr>
                                <th>论文主题</th>
                                <th>作者</th>
                                <th>论文类型</th>
                                <th>发表时间</th>
                                <th>修改时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody id="table">
<!--                            <tr align="center">&ndash;&gt;-->
<!--                                <td>1</td>-->
<!--                                <td>2</td>-->
<!--                                <td>3</td>-->
<!--                                <td>4</td>-->
<!--                                <td>5</td>-->
<!--                                <td>删除</td>-->
<!--                            </tr>-->
                        </tbody>

                    </table>
                </form>
            </td>
        </tr>
    </table>
</div>
</body>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script >
$(function (){
    // alert(1)
    /**
     *  加载下拉框
     */
    $.ajax({
        "url": '/selectOption',
        "type": "post",
        "dataType": "json",
        "success": function (a) {
            for(var i = 0 ; i < a.length ; i++){
                console.log(a);
                var html = "<option>"+a[i].typeCode+"</option>";
                // console.log("html="+html);
                $("select").append(html);
            }
        },
        error: function () {
            alert("/selectOption 加载失败");
            // window.location.href = "./index.html";
        }

    });

    /**
     * 模糊查询论文
     */
    $("#btn").submit(function () {
        var title = $('input[type=text]').val();
        var typeCode = $('#select option:selected').val();

        $("#table").html('已清空');

        $.ajax({
            "url": '/fuzzyQuery',
            "async":false,
            "data": {
                "title":    title,
                "typeCode": typeCode
            },
            "type": "post",
            "dataType": "json",
            "success": function (a) {
                alert("title="+title+",typeCode="+typeCode);
                //清空表格内容
                alert('a.length='+a.length);
                var html=
                    "<tr>\n" +
                    "<th>论文主题</th>\n" +
                    "<th>作者</th>\n" +
                    "<th>论文类型</th>\n" +
                    "<th>发表时间</th>\n" +
                    "<th>修改时间</th>\n" +
                    "<th>操作</th>\n" +
                    "</tr>";
                for(var i = 0 ; i < a.length ; i++){
                    alert('i='+i);
                    console.log('a='+a);
                    html = html+
                        "<tr align=\"center\">" +
                        "<td>"+a[i].title+"</td>" +
                        "<td>"+a[i].createdBy+"</td>" +
                        "<td>"+a[i].typeCode+"</td>" +
                        "<td>"+a[i].creationDate+"</td>" +
                        "<td>"+a[i].modifyBy+"</td>"
                    "<td><a href='#'>修改</a></td>" +
                    "</tr>";
                    console.log("html="+html);
                    // $("#table").append(html);
                    $("#table").append(html);
                }
            },
            error: function () {
                alert("/fuzzyQuery 加载失败");
                // window.location.href = "./index.html";
            }
        });

    });


    /**
     * 查询全部论文
     */
    $.ajax({
        "url": '/paperlist',
        "type": "post",
        "dataType": "json",
        "success": function (a) {

            for(var i = 0 ; i < a.length ; i++){
                console.log(a);
                var html = "<tr align=\"center\">" +
                    "<td>"+a[i].title+"</td>" +
                    "<td>"+a[i].createdBy+"</td>" +
                    "<td>"+a[i].typeCode+"</td>" +
                    "<td>"+a[i].creationDate+"</td>" +
                    "<td>"+a[i].modifyBy+"</td>"+
                    "<td><a href='#' onclick='delete1("+a[i].id+")'>删除</a><a href='#'>修改</a></td>" +
                    "</tr>";
                // console.log("html="+html);
                $("#table").append(html);
            }
        },
        error: function () {
            alert("加载失败");
            // window.location.href = "./index.html";
        }
    });


});
/**
 * 删除操作
 */
function delete1(id){
    if(confirm("确认要删除吗？"))
        $.ajax({
            "url": '/delete',
            "type": "get",
            "data":{"id":id},
            "dataType": "json",
            "success": function (a) {
                console.log("a="+a);
                if(a>0){
                    window.location.reload();
                    alert("删除成功");
                }else{
                    alert("失败");
                }

            },
            error: function () {
                alert("加载失败");
                // window.location.href = "./index.html";
            }
        });

}


</script>

</html>